'use client'

import React, { useState } from 'react';
import { 
  Button, 
  Dropdown, 
  DropdownTrigger, 
  DropdownMenu, 
  DropdownItem,
  Tooltip,
  useDisclosure
} from '@nextui-org/react';
import { BiDotsVerticalRounded, BiEdit, BiTrash } from 'react-icons/bi';
import { useRouter } from 'next/navigation';
import ToastUtil from '@/app/utils/toastUtil';
import { deleteNote } from '@/app/service/noteService';
import DelNoteService from '@/app/service/components/delNoteService';

interface NoteListActionsProps {
  noteId: number;
  collectionId: string | null;
  onDeleteSuccess?: () => void;
}

export default function NoteListActions({ noteId, collectionId, onDeleteSuccess }: NoteListActionsProps) {
  const router = useRouter();
  
  // 删除笔记相关状态
  const { isOpen: isOpenDelNote, onOpen: onOpenDelNote, onOpenChange: onOpenChangeDelNote } = useDisclosure();
  
  // 处理编辑笔记
  const handleEdit = () => {
    router.push(`/note/${noteId}?collectionId=${collectionId || 'root'}`);
  };
  
  return (
    <div className="flex items-center justify-center relative gap-2">
      <Tooltip content="编辑笔记">
        <Button
          isIconOnly
          size="sm"
          variant="light"
          onPress={handleEdit}
        >
          <BiEdit className="text-blue-500" />
        </Button>
      </Tooltip>
      
      <div className="relative flex justify-center items-end">
        <Dropdown backdrop="transparent" className="bg-background border-1 border-default-200">
          <DropdownTrigger>
            <Button
              isIconOnly
              size="sm"
              variant="light"
            >
              <BiDotsVerticalRounded />
            </Button>
          </DropdownTrigger>
          <DropdownMenu onAction={(key) => {
            if (key === 'delete') {
              onOpenDelNote();
            }
          }}>
            <DropdownItem 
              key="delete" 
              className="text-danger" 
              color="danger" 
              startContent={<BiTrash />}
            >
              删除
            </DropdownItem>
          </DropdownMenu>
        </Dropdown>
      </div>
      
      {/* 删除笔记的确认弹窗 */}
      <DelNoteService 
        isOpenDelNote={isOpenDelNote} 
        onOpenChangeDelNote={onOpenChangeDelNote} 
        noteId={noteId} 
        onDeleteSuccess={onDeleteSuccess}
      />
    </div>
  );
} 